package com.bokey.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bokey.entity.Follow;
import com.bokey.entity.User;
import com.bokey.service.FollowService;
import com.bokey.service.NodeService;
import com.bokey.service.UserService;
import com.bokey.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin // 跨域解决
@RestController
@RequestMapping("/follow")
public class FollowController {

    @Autowired
    private NodeController nodeController;
    @Autowired
    private NodeService nodeService;
    @Autowired
    private UserService userService;
    @Autowired
    private FollowService followService;

    // 通过id关注其他用户
    @RequestMapping("/attentionById/{followerId}/{beFollowerId}")
    public Integer attentionById(@PathVariable Integer followerId,@PathVariable Integer beFollowerId ){
        Follow follow = new Follow();
        follow.setFollowerid(followerId);
        follow.setBefollowerid(beFollowerId);
        if(!followService.save(follow)) {
            System.out.println("数据库存入失败！");
            return 0;
        }
        System.out.println("数据库存入成功！");
        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("beFollowerId",beFollowerId).eq("followerId",followerId);
        Follow one = followService.getOne(queryWrapper);
        return one.getId();
    }

    // 通过id(followId)取消关注其他用户
    @RequestMapping("/notAttentionById/{id}")
    public boolean notAttentionById(@PathVariable Integer id){
        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return followService.remove(queryWrapper);
    }

    // 通过id查询两个用户是否是关注关系，返回关注id
    @RequestMapping("/whetherAttention/{followerId}/{beFollowerId}")
    public Integer whetherAttention(@PathVariable Integer followerId,@PathVariable Integer beFollowerId){
        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("followerId",followerId).eq("beFollowerId",beFollowerId);
        Follow follow = followService.getOne(queryWrapper);
        System.out.println("关注关系："+follow);
        if(follow==null) return 0; // 若找不到该关注关系返回0
        return follow.getId(); // 若找到该关注关系返回该关系的id
    }

    // 用userId(beFollowerId)查找该用户的粉丝数量
    @RequestMapping("/getFollowerNumById/{beFollowerId}")
    public long getFollowerNumById(@PathVariable Integer beFollowerId){
        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("beFollowerId",beFollowerId);
        return followService.count(queryWrapper);
    }

    // 用userId(FollowerId)查找该用户关注的对象，并得到该对象的相关信息(用户id，用户名，用户描述，笔记数量，粉丝数量)
    @RequestMapping("/getBeFollowerMsgById/{followerId}")
    public R getBeFollowerMsgById(@PathVariable Integer followerId){
        // 获取该用户所有关注的人的id
        QueryWrapper<Follow> followQueryWrapper = new QueryWrapper<>();
        followQueryWrapper.eq("followerId",followerId);
        List<Follow> beFollows = followService.list(followQueryWrapper);
        List<User> users = new ArrayList<>();
        List<Long> nodeNums = new ArrayList<>();
        List<Long> followerNums = new ArrayList<>();
        for (Follow item:beFollows) {
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("id",item.getBefollowerid());
            // 查询得到每个被关注人的信息
            User user = userService.getOne(userQueryWrapper);
            // 将每个被关注人放入list
            users.add(user);
            // 查询每个被关注人的笔记数量，粉丝数量
            long nodeNum = nodeController.getUserNodeNumByUserId(user.getId());
            long followerNum = getFollowerNumById(user.getId());
            // 将它们放入list
            nodeNums.add(nodeNum);
            followerNums.add(followerNum);
        }
        // 返回给前端
        return R.ok().data("beFollows",beFollows).data("users",users).data("nodeNums",nodeNums).data("followerNums",followerNums);
    }

    // 用userId(beFollowerId)查找该用户的粉丝，并得到粉丝的相关信息(用户id，用户名，用户描述，笔记数量，粉丝数量)
    @RequestMapping("/getFollowerMsgById/{beFollowerId}")
    public R getFollowerMsgById(@PathVariable Integer beFollowerId){
        // 获取该用户所有粉丝的id
        QueryWrapper<Follow> followQueryWrapper = new QueryWrapper<>();
        followQueryWrapper.eq("beFollowerId",beFollowerId);
        List<Follow> follows = followService.list(followQueryWrapper);
        List<User> users = new ArrayList<>();
        List<Long> nodeNums = new ArrayList<>();
        List<Long> followerNums = new ArrayList<>();
        for (Follow item:follows) {
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("id",item.getFollowerid());
            // 查询得到每个粉丝的信息
            User user = userService.getOne(userQueryWrapper);
            // 将每个粉丝放入list
            users.add(user);
            // 查询每个粉丝的笔记数量，粉丝数量
            long nodeNum = nodeController.getUserNodeNumByUserId(user.getId());
            long followerNum = getFollowerNumById(user.getId());
            // 将它们放入list
            nodeNums.add(nodeNum);
            followerNums.add(followerNum);
        }
        return R.ok().data("follows",follows).data("users",users).data("nodeNums",nodeNums).data("followerNums",followerNums);
    }

}
